package com.thrd.taiexhibition;

import android.app.Application;
import android.content.ComponentCallbacks2;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

import com.alibaba.android.arouter.launcher.ARouter;
import com.facebook.cache.disk.DiskCacheConfig;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import com.facebook.imagepipeline.core.ImagePipelineFactory;
import com.thrd.basicframework.media.MusicService;
import com.thrd.basicframework.media.MusicServiceConnection;
import com.thrd.basicframework.utils.ProxyCache;
import com.thrd.basicframework.utils.SpUtil;
import com.thrd.taiexhibition.bean.BeanCity;
import com.thrd.taiexhibition.bean.BeanCityList;
import com.thrd.taiexhibition.bean.BeanSystemInfo;
import com.thrd.taiexhibition.fresco.FrescoUtils;

import java.util.List;

public class BaseApplication extends Application {
    /**
     * ARouter 开关log
     */
    private static final boolean isDebugARouter=true;

    /**
     * ARouter 开关log
     */
    public static  Context mContext;
//    /**
//     * 判断是否第一次进入
//     */
//    private int mFinalCount;

    public BeanSystemInfo mBeanSystemInfo;

    private long cityId=1;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext=this;
        initARouter();
        initMMKV();
        initFresco();
        FrescoUtils.getInstance().initFresco(this);
        bindService();

        ProxyCache.getInstance().init(mContext);



    }
    private MusicService mMusicService;
    private MusicServiceConnection.OnMusicServiceConnection musicServiceConnection=new MusicServiceConnection.OnMusicServiceConnection() {
        @Override
        public void onMusicServiceConnection(IBinder service) {
            if(service!=null){
                mMusicService = ((MusicService.MusicBinder) service).getService();
            }
        }
    };
    //音乐播放器服务
    public MusicService getMusicService() {
        if(mMusicService==null){
            bindService();
        }
        return mMusicService;
    }

    /**
     * 腾讯存储框架初始化
     */
    private void initMMKV() {
//        MMKV.initialize(this);
//        MMKV.defaultMMKV();
//        SpUtil.mmkv.encode()
        SpUtil.Companion.getInstance(this);
    }

    /**
     * 初始化ARouter，模块化跳转
     */
    private void initARouter() {
        if(isDebugARouter){
            ARouter.openLog();
            ARouter.openDebug();
        }
        ARouter.init(BaseApplication.this);
    }

    /**
     * OnTrimMemory是Android 4.0之后提供的API，系统会根据不同的内存状态来回调。
     */
    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) { // 60
            ImagePipelineFactory.getInstance().getImagePipeline().clearMemoryCaches();
        }
    }
    /**
     * OnLowMemory是Android提供的API，在系统内存不足，所有后台程序
     * （优先级为background的进程，不是指后台运行的进程）都被杀死时，系统会调用OnLowMemory。
     */
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        ImagePipelineFactory.getInstance().getImagePipeline().clearMemoryCaches();
    }

    private void initFresco(){
        ImagePipelineConfig.Builder imagePipelineConfigBuilder = ImagePipelineConfig.newBuilder(mContext);
        imagePipelineConfigBuilder.setMainDiskCacheConfig(DiskCacheConfig.newBuilder(mContext)
                .setBaseDirectoryPath(mContext.getExternalCacheDir())//设置磁盘缓存的路径
                .setBaseDirectoryName("image")//设置磁盘缓存文件夹的名称
                .setMaxCacheSize(1024 * 1024 * 1024)//设置磁盘缓存的大小
                .build());
        ImagePipelineConfig config = imagePipelineConfigBuilder.build();
        Fresco.initialize(mContext, config);//初始化
    }

    private void bindService(){
        Intent intent = new Intent(this, MusicService.class);
        bindService(intent, new MusicServiceConnection(musicServiceConnection), BIND_AUTO_CREATE);
    }



    public long getCityId() {
        return cityId;
    }

    public void setCityId(long cityId) {
        this.cityId = cityId;
    }

    public BeanSystemInfo getBeanSystemInfo() {
        return mBeanSystemInfo;
    }

    public void setBeanSystemInfo(BeanSystemInfo mBeanSystemInfo) {
        this.mBeanSystemInfo = mBeanSystemInfo;
    }

    //获取热门城市
    public List<BeanCity> getHotCityList(){
        if(mBeanSystemInfo!=null){
            return   mBeanSystemInfo.getHotCityList();
        }else{
            return null;
        }
    }

    public BeanCityList getBeanCityList(){
        if(mBeanSystemInfo!=null){
            return   mBeanSystemInfo.getBeanCityLists();
        }else{
            return null;
        }
    }
}
